package com.crm.service.controller;

import com.crm.common.pojo.Service;
import com.crm.global.exception.ExceptionEnum;
import com.crm.global.result.Message;
import com.crm.service.service.TblServiceImpl;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Api(tags = "服务管理")
@RestController
@RequestMapping("/service")
public class ServiceController {

    @Autowired
    private TblServiceImpl tblServiceImpl;

    @ApiOperation("服务创建")
    @PostMapping("/save")
    public  ResponseEntity<Message<String>> save(@RequestBody(required = false) Service tblService) {
        tblServiceImpl.saveOrUpdate(tblService);
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS));
    }



    @ApiOperation("查询所有的服务")
    @PostMapping("/findAll")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true,paramType = "query"),
            @ApiImplicitParam(name = "size",value = "页面大小",required = true,paramType = "query")
    })
    public ResponseEntity<Message<PageInfo<Service>>> findAll(int page, int size, @RequestBody(required = false)  Service tblService) {
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS,tblServiceImpl.findAll(page, size, tblService)));
    }

    @ApiOperation("删除服务")
    @GetMapping("/delete")
    @ApiImplicitParam(name = "id",value = "服务id",required = true,paramType = "query")
    public ResponseEntity<Message<String>> delete(Long id) {
        tblServiceImpl.deleteById(id);
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS));
    }

    @ApiOperation("查询服务")
    @GetMapping("/selectById")
    @ApiImplicitParam(name = "id",value = "服务id",required = true,paramType = "query")
    public ResponseEntity<Message<Service>> findById(Long id) {
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS,tblServiceImpl.selectById(id)));
    }

    @ApiOperation("服务处理")
    @GetMapping("/detail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "服务id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "result",value = "处理结果",required = true,paramType = "query")
    })
    public ResponseEntity<Message<String>> detail(Long id,String result) {
        Service tblService = tblServiceImpl.selectById(id);
        tblService.setResult(result);
        tblServiceImpl.saveOrUpdate(tblService);
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS));
    }


    @ApiOperation("服务分配")
    @GetMapping("/allocation")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceId",value = "服务id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "userId",value = "用户id",required = true,paramType = "query")
    })
    public ResponseEntity<Message<String>> allocation(Long serviceId, Long userId) {
        Service tblService = tblServiceImpl.selectById(serviceId);
        tblService.setDueTo(userId);
        tblServiceImpl.saveOrUpdate(tblService);
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS));
    }

    @ApiOperation("服务反馈")
    @GetMapping("/feedback")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "服务id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "content",value = "反馈内容",required = true,paramType = "query"),
            @ApiImplicitParam(name = "satisfy",value = "满意度",required = true,paramType = "query"),
    })
    public ResponseEntity<Message<String>> feedback(Long id,String content,String satisfy) {
        Service tblService = tblServiceImpl.selectById(id);
        tblService.setFeedback(content);
        tblService.setSatisfy(satisfy);
        tblServiceImpl.saveOrUpdate(tblService);
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS));
    }
}
